package com.wzz.seckillone.service.impl;



import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzz.seckillone.mapper.GoodsMapper;
import com.wzz.seckillone.mapper.OrderMapper;
import com.wzz.seckillone.mapper.SeckillGoodsMapper;
import com.wzz.seckillone.mapper.SeckillOrderMapper;
import com.wzz.seckillone.pojo.*;
import com.wzz.seckillone.service.OrderService;
import com.wzz.seckillone.service.SeckillGoodsService;
import com.wzz.seckillone.service.SeckillOrderService;
import com.wzz.seckillone.utils.SnowFlake;
import com.wzz.seckillone.vo.GoodsVo;
import com.wzz.seckillone.vo.OrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-11-08
 */
@Service
@Transactional
@Slf4j
public class SeckillOrderServiceImpl extends ServiceImpl<SeckillOrderMapper, SeckillOrder> implements SeckillOrderService {


    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SeckillOrderMapper seckillOrderMapper;
    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    /**
     * @Method
     * @Author wzz
     * @Version  1.0
     * @Description 根据userid 和  商品id查询数据
     * @param
     * @Return
     * @Exception
     * @Date 2022/3/18 20:32
     */
    @Override
    public SeckillOrder getSeckillOrderByGoodsIdAndUserId(String goodsId, String userId) {
        return seckillOrderMapper.getSeckillOrderByGoodsIdAndUserId(goodsId,userId);
    }

    @Override
    public Order doSecKill(User user, GoodsVo goodsVo) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        //秒杀商品表
        SeckillGoods secKillGoods = seckillGoodsMapper.getSeckillGoodsByGoodsId(goodsVo.getId());
        if (secKillGoods.getStockCount() <1){
            valueOperations.set("isStockEmpty:"+goodsVo.getId(),true);
            return null;
        }
        //扣库存 同步更新商品信息
        seckillGoodsMapper.updateOneCount(goodsVo.getId());

        Goods good = goodsMapper.getGoodsByGoodsId(goodsVo.getId());
        good.setGoodsStock(good.getGoodsStock()-1);
        goodsMapper.updateGoodsCount(good);


        //保存订单信息
        Order order = new Order();
        order.setGoodsId(goodsVo.getId());
        order.setUserId(user.getUserId());
        order.setId(String.valueOf(SnowFlake.nextId()));
        order.setCreateData(new Date());
        order.setOrderChannel(0);
        order.setDeliveryAddrId("test");
        order.setGoodsCount(1);
        order.setGoodsPrice(goodsVo.getSeckillPrice());
        order.setStatus(0);//0未支付 1支付
        order.setPayDate(new Date());
        orderMapper.saveOrder(order);

        //保存秒杀订单信息
        SeckillOrder secOrder = new SeckillOrder();
        String l = String.valueOf(SnowFlake.nextId());
        secOrder.setId(l);
        secOrder.setGoodsId(goodsVo.getId());
        secOrder.setUserId(user.getUserId());
        secOrder.setOrderId(l);
        seckillOrderMapper.saveOrder(secOrder);
        //解决当同一个id 同时发送多个req的时候 同一id只能买一个  把userid+goods作为联合主键 存入redis
        redisTemplate.opsForValue().set("order"+user.getUserId()+":"+goodsVo.getId(),secOrder);

        return order;
    }

    /**
     * @Method
     * @Author wzz
     * @Version  1.0
     * @Description订单和商品信息
     * @param
     * @Return
     * @Exception
     * @Date 2022/3/21 16:45
     */
    @Override
    public OrderVo getOrderDetailsByOrderId(String orderId) {
        SeckillOrder secKillOrder = seckillOrderMapper.getOrderByOrderId(orderId);
        Order order = orderMapper.getOrderByUserIdAndGoodsId(secKillOrder.getUserId(),secKillOrder.getGoodsId());
        //Order order = orderMapper.getOrderByOrderId(orderId);
        GoodsVo goodsVo = goodsMapper.findGoodsVoById(order.getGoodsId());
        OrderVo orderVo = new OrderVo();
        orderVo.setOrder(order);
        orderVo.setGoodsVo(goodsVo);
        return orderVo;
    }

    /**
     * @Method
     * @Author wzz
     * @Version  1.0
     * @Description 秒杀是否成功的查询
     * @param
     * @Return
     * @Exception
     * @Date 2022/3/22 15:20
     */
    @Override
    public String getResult(User user, String goodsId) {
        SeckillOrder seckillOrder = seckillOrderMapper.getSeckillOrderByGoodsIdAndUserId(goodsId, user.getUserId());
        log.info("getResultServiceGetResult"+seckillOrder);
        if (seckillOrder != null){
            log.info(this+"seckill有数据 秒杀成功");
            return seckillOrder.getOrderId();
        }else if(redisTemplate.hasKey("isStockEmpty"+goodsId)){
            //如果redis中有这个key  代表商品已经没有库存了
            log.info(this+"无库存");
            return "-1";
        }else if(redisTemplate.hasKey("order" + user.getUserId() + ":" + goodsId)){
            //redis中 如果有goodsis和userId的组合键 则返回只能购买一份
            return "1";
        }else{
            log.info(this+"等待中");
            return "0";
        }
    }


}
